﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DALLayer.Base;
using DataDefinition;
namespace DALLayer.Providers
{
    public class CommentDataProvider : DataAccessBase
    {
        #region [Constructor]
        /// <summary>
        /// Initializes a new instance of the <see cref="CommentDataProvider" /> class.
        /// </summary>
        public CommentDataProvider()
        {

        } 
        #endregion

        #region [Public Methods]

        /// <summary>
        /// Gets comments by article id
        /// </summary>
        /// <param name="articleId">the article Id</param>
        /// <returns>data set</returns>
       public AutoFixDataSet GetCommentByArticleID(int articleID)
        {
            AutoFixDataSet dataSet = null;

            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class
                base.CommandTextBuilder.Append(@"SELECT  Comment.Comment, ApplicationUser.UserName, ApplicationUser.ProfilePictureURL, Comment.CommentID");
                base.CommandTextBuilder.Append(@" FROM (ApplicationUser INNER JOIN (Article INNER JOIN
                         Comment ON Article.ArticleID = Comment.ArticleID) ON ApplicationUser.UserID = Comment.UserID)");
                base.CommandTextBuilder.Append(@"WHERE (Comment.IsActive = True) AND (ApplicationUser.IsActive = True) AND (Article.ArticleID = @ArticleID) AND (Article.IsActive = True)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@ArticleID", articleID);
                bool success = false;
                //Check if it is success
                success = base.ExecuteReader();
                //If it is successful
                if (success == true)
                {
                    //Initial the dataset
                    dataSet = new AutoFixDataSet();
                    //Fill the table
                    dataSet.GetCommentArticleID.Load(base.DataReader);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            finally
            {
                //Close connection
                base.FinalizeConnection();
            }
            //Return the filled dataset
            return dataSet;
        }

       
       /// <summary>
        /// The method inserts comments to the table
       /// </summary>
       /// <param name="comment">comment</param>
       /// <param name="userId">user id</param>
       /// <param name="articleId">article id</param>
       /// <param name="isActive">is active</param>
       /// <returns>true or false</returns>
        public bool InsertComment(string comment, int userId, int articleId, bool isActive)
        {

            bool success = false;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append(@"
                    INSERT INTO Comment(Comment, UserID, ArticleID, IsActive)
                    VALUES(@Comment, @UserID, @ArticleId, @IsActive)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@Comment", comment);
                base.DBParams.Add(@"@UserID", userId);
                base.DBParams.Add(@"@ArticleId", articleId);
                base.DBParams.Add(@"@IsActive", isActive);

                //Check if it is success
                success = base.ExecuteNonQuery();

            }
            catch (Exception)
            {
                success = false;
                //Log here
            }
            //return success
            return success;
        }

       /// <summary>
        /// The method updates the comment based on a given comment id
       /// </summary>
       /// <param name="commentId">comment id</param>
       /// <param name="comment">comment</param>
       /// <param name="userId">user id</param>
       /// <param name="articleId">article id</param>
       /// <param name="isActive">is active</param>
       /// <returns>true or false</returns>      
        public bool UpdateComment(int commentId, string comment, int userId, int articleId, bool isActive)
        {
            bool success = false;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append(@"
                    UPDATE Comment
                    SET Comment = @Comment, 
                        UserID =  @UserID, 
                        ArticleID = @ArticleId, 
                        IsActive = @IsActive
                    WHERE CommentID = @CommentId");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@CommentId", commentId);
                base.DBParams.Add(@"@Comment", comment);
                base.DBParams.Add(@"@UserID", userId);
                base.DBParams.Add(@"@ArticleId", articleId);
                base.DBParams.Add(@"@IsActive", isActive);

                //Check if it is success
                success = base.ExecuteNonQuery();

            }
            catch (Exception)
            {
                success = false;
                //Log here
            }
            //return success
            return success;

        }
        
        #endregion
    }
}
